package com.zhiyume.service;

import com.zhiyume.dao.api.TAdminMapper;
import com.zhiyume.dao.entity.TAdmin;
import com.zhiyume.dao.entity.TAdminCriteria;
import com.zhiyume.service.util.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jiaxz
 * @date 2017/12/10 14:39
 * @description
 */
@Service
public class ModifyPasswdService {
    @Autowired
    private TAdminMapper tAdminMapper;
    public int modifyPasswd(long id,String oldPassword,String password) {
        TAdminCriteria example = new TAdminCriteria();
        TAdminCriteria.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        List<TAdmin> list = tAdminMapper.selectByExample(example);
        if(null != list && list.size() > 0) {
            TAdmin  tadmin = list.get(0);
            if(!EncryptUtil.SHA256(oldPassword).equals(tadmin.getPassword())){
                return -1;
            }
            tadmin.setPassword(EncryptUtil.SHA256(password));
           return tAdminMapper.updateByPrimaryKey(tadmin);
        }
        return 0;
    }
}
